#include "loginwidget.h"


LoginWidget::LoginWidget(QWidget *parent) : QWidget(parent)
{
    //登录窗口的定义
    closebtn=new QPushButton(this);
    loginlabel=new QLabel("账号",this);
    passwordlabel=new QLabel("密码",this);
    loginbtn=new QPushButton("登录",this);
    loginline=new QLineEdit(this);
    passwordline=new QLineEdit(this);
    QHBoxLayout*hlayout1=new QHBoxLayout;
    QHBoxLayout*hlayout2=new QHBoxLayout;
    QHBoxLayout*hlayout3=new QHBoxLayout;
    QHBoxLayout*hlayout4=new QHBoxLayout;
    QHBoxLayout*hlayout5=new QHBoxLayout;
    QVBoxLayout*vlayout=new QVBoxLayout(this);
    remember=new QCheckBox("记住密码",this);
    regex=QRegularExpression("^(?=.*[A-Za-z0-9])[A-Za-z0-9]{8,16}$");
    QRegularExpressionValidator *validator = new QRegularExpressionValidator(regex, this);
    setWindowTitle("登录界面");

    //文本大小设置
    closebtn->setIcon(QIcon(":/icon/close.png"));
    loginline->setFixedSize(loginline->width()*4,loginline->height()*1.3);
    passwordline->setFixedSize(passwordline->width()*4,passwordline->height()*1.3);
    loginline->setPlaceholderText("账号");
    loginline->setToolTip("账号");
    loginline->setValidator(validator);
    passwordline->setMaxLength(16);
    passwordline->setEchoMode(QLineEdit::Password);
    passwordline->setPlaceholderText("密码");
    passwordline->setToolTip("密码");
    QFont f1("",12),f2("",16,QFont::Bold);
    f1.setFamily("SimSun");
    loginline->setFont(f1);
    passwordline->setFont(f1);
    loginlabel->setFont(f2);
    passwordlabel->setFont(f2);
    loginbtn->setFixedSize(200,50);
    loginbtn->setFont(f1);

    //布局
    hlayout5->addStretch(1);
    hlayout5->addWidget(closebtn);
    hlayout1->addStretch(1);
    hlayout1->addWidget(loginlabel);
    hlayout1->addWidget(loginline);
    hlayout1->addStretch(1);
    hlayout2->addStretch(1);
    hlayout2->addWidget(passwordlabel);
    hlayout2->addWidget(passwordline);
    hlayout2->addStretch(1);
    hlayout3->addStretch(1);
    hlayout3->addWidget(loginbtn);
    hlayout3->addStretch(1);
    hlayout4->addStretch(1);
    hlayout4->addWidget(remember);
    //hlayout4->addWidget(link);
    hlayout4->addStretch(1);
    vlayout->addLayout(hlayout5);
    vlayout->addStretch(4);
    vlayout->addLayout(hlayout1);
    vlayout->addLayout(hlayout2);
    vlayout->addLayout(hlayout4);
    vlayout->addLayout(hlayout3);
    vlayout->addStretch(5);
    vlayout->setSpacing(50);

    //背景
    setFixedSize(600,400);
    // QPixmap pixmap=QPixmap(":/background/preview.jpg").scaled(this->size());

    // QPalette palette;
    // palette.setBrush(QPalette::Window,QBrush(pixmap));
    // //setPalette(palette);

    //控件美化
    QAction*id=new QAction(this);
    id->setIcon(QIcon(":/icon/User.jpg"));
    QAction*lock=new QAction(this);
    lock->setIcon(QIcon(":/icon/lock.png"));
    invisible=new QAction(this);
    invisible->setIcon(QIcon(":/icon/Invisible .jpg"));
    visible=new QAction(this);
    visible->setIcon(QIcon(":/icon/Visible.jpg"));
    loginline->addAction(id,QLineEdit::LeadingPosition);
    passwordline->addAction(lock,QLineEdit::LeadingPosition);
    passwordline->addAction(invisible,QLineEdit::TrailingPosition);


    loginline->setStyleSheet("QLineEdit{border-width:0,border-style:outset}");
    loginlabel->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);
    passwordlabel->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);
    loginline->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);
    passwordline->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);
    loginbtn->setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);



    //连接设置
    connect(loginbtn,&QPushButton::clicked,this,&LoginWidget::Login);
    //connect(passwordline,&QLineEdit::returnPressed,this,&LoginWidget::Login);
    connect(remember,&QCheckBox::clicked,this,&LoginWidget::RemberPassword);
    connect(invisible,&QAction::triggered,this,&LoginWidget::InvisibleToVisible);
    connect(visible,&QAction::triggered,this,&LoginWidget::VisibleToInvisible);
    connect(closebtn,&QPushButton::clicked,this,&QWidget::close);

    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

}

void LoginWidget::Login()
{
    emit login(loginline->text(),passwordline->text());
}

void LoginWidget::RemberPassword()
{


}

void LoginWidget::VisibleToInvisible()
{
    passwordline->removeAction(visible);
    passwordline->addAction(invisible,QLineEdit::TrailingPosition);
    passwordline->setEchoMode(QLineEdit::Password);
}

void LoginWidget::InvisibleToVisible()
{
    passwordline->removeAction(invisible);
    passwordline->addAction(visible,QLineEdit::TrailingPosition);
    passwordline->setEchoMode(QLineEdit::Normal);
}

void LoginWidget::keyPressEvent(QKeyEvent *event)
{
    if(event->key()==Qt::Key_Enter||event->key()==Qt::Key_Return)
        Login();
}

void LoginWidget::paintEvent(QPaintEvent* e)
{
    Q_UNUSED(e)
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setBrush(QColor("#f0f0f0")); // 背景颜色
    painter.setPen(Qt::NoPen); // 无边框
    painter.drawRoundedRect(this->rect(), 10, 10);
}

void LoginWidget::mousePressEvent(QMouseEvent*e)
{
    QWidget::mousePressEvent(e);
    setFocus();
    if(e->button()==Qt::LeftButton)
    {
        isdrag=true;
        startpoint=e->globalPosition();
    }
}

void LoginWidget::mouseReleaseEvent(QMouseEvent*e)
{
    if (e->button() == Qt::LeftButton)
    {
        isdrag = false;
    }
}

void LoginWidget::mouseMoveEvent(QMouseEvent*e)
{

    if(isdrag)
    {
        QPointF movepoint=e->globalPosition();
        QPointF distance=movepoint-startpoint;
        QPoint mpoint=this->pos();
        this->move(mpoint+distance.toPoint());
        startpoint=movepoint;
    }

}
